<template>
    <view>
        <!-- <back></back> -->
        <view class="out">
            <view
                class="card u-flex-center u-row-between"
                v-for="(item, index) in list"
                :key="index"
                @click="back(item)"
            >
                <view class="u-flex-center">
                    <view class="head-img u-flex-center">
                        {{ item.name[0] }}
                    </view>
                    <view>
                        <view class="user-message u-flex">
                            <view>{{ item.name }}</view>
                            <text>{{ item.mobile }}</text>
                            <text
                                style="font-size: 22rpx; color: #606dce; margin-left: 30rpx"
                                v-if="item.default == 1"
                                >默认</text
                            >
                        </view>
                        <view class="address u-flex">
                            <image src="@/static/yCreate/icon.png" mode=""></image>
                            <view class="detail u-line-1"
                                >{{ item.province }} {{ item.city }} {{ item.district }}
                                {{ item.address }}
                            </view>
                        </view>
                    </view>
                </view>
                <image
                    src="@/static/yCreate/icon2.png"
                    mode="widthFix"
                    class="icon"
                    @click.stop="
                        jump('/yCreate/pages/addAddress??type=edit&data=' + JSON.stringify(item))
                    "
                ></image>
                <image
                    src="@/static/yCreate/del.png"
                    mode="widthFix"
                    class="icon"
                    @click.stop="del(item)"
                ></image>
            </view>
        </view>
        <view style="height: 178rpx; width: 100%"></view>
        <view class="btn u-flex-center" @click="jump('/yCreate/pages/addAddress?type=add')"
            >新增收货地址</view
        >
    </view>
</template>

<script>
export default {
    data() {
        return {
            ids: [],
            list: [],
            isNull: false,
        };
    },
    onLoad(options) {
        console.log(JSON.parse(options.id));
        if (options.id) {
            this.ids = JSON.parse(options.id);
        }
        if (options.isNull) {
            this.isNull = options.isNull;
        }
    },
    onShow() {
        this.getList();
    },
    methods: {
        del(item) {
            this.$u
                .post("/api/wanlshop/address/deladdress", {
                    id: item.id,
                })
                .then((res) => {
                    if (res.code == 1) {
                        this.$u.toast(res.msg);
                        this.getList();
                    }
                });
        },
        back(item) {
            this.$u
                .post("/api/wanlshop/order/judge_address", {
                    address_id: item.id,
                    data: this.ids,
                })
                .then((res) => {
                    if (res.code == 1) {
                        let pages = getCurrentPages(); //获取所有页面栈实例列表
                        let prevPage = pages[pages.length - 2]; //上一页页面实例
                        if (this.isNull) {
                            prevPage.$vm.isNull = this.isNull; //修改上一页参数值
                        }
                        prevPage.$vm.addressDetail = item; //修改上一页参数值
                        uni.navigateBack();
                    } else {
                        uni.showToast({
                            title: "抱歉您选择商品不在配送范围内",
                            icon: "none",
                        });
                    }
                });
        },
        getList() {
            this.$u.get("/api/wanlshop/address/getaddress").then((res) => {
                this.list = res.data.data;
            });
        },
        jump(url) {
            uni.navigateTo({
                url: url,
            });
        },
    },
};
</script>

<style lang="scss">
.out {
    padding: 38rpx 30rpx;

    .card {
        width: 100%;
        background: #ffffff;
        border-radius: 20rpx;
        padding: 30rpx;
        margin-bottom: 20rpx;

        .head-img {
            width: 90rpx;
            height: 90rpx;
            background: #606dce;
            border-radius: 50rpx;
            margin-right: 18rpx;
            font-size: 32rpx;
            color: #fff;
            flex-shrink: 0;
        }

        .user-message {
            font-size: 32rpx;

            view {
                margin-right: 18rpx;
            }

            text {
                font-size: 24rpx;
                color: #b0b0b0;
            }
        }

        .address {
            font-size: 28rpx;

            image {
                margin-right: 10rpx;
                width: 20rpx;
                height: 26rpx;
            }

            .detail {
                width: 400rpx;
            }
        }

        .icon {
            width: 28rpx;
            height: auto;
        }
    }
}

.btn {
    position: fixed;
    bottom: 78rpx;
    left: 50%;
    transform: translateX(-50%);
    width: 632rpx;
    height: 80rpx;
    background: linear-gradient(98deg, #5d64ca 0%, #6f9bdf 100%);
    border-radius: 40rpx;
    font-size: 30rpx;
    color: #ffffff;
}
</style>
